package com.getpebble.android.framework.protocol.outbound;

import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;
import com.google.common.primitives.UnsignedInteger;
import java.util.TimeZone;

public class PblSetTimeMessage extends PblOutboundMessage {
    private UnsignedInteger mCurrentTime;

    public enum TimeCommand {
        UNKNOWN((byte) -1),
        GET_TIME_REQ((byte) 0),
        GET_TIME_RESP((byte) 1),
        SET_TIME_REQ((byte) 2);
        
        private byte command;

        private TimeCommand(byte command) {
            this.command = command;
        }

        public byte getCommand() {
            return this.command;
        }
    }

    public PblSetTimeMessage() {
        super(EndpointId.TIME);
    }

    public UnsignedInteger getCurrentTime() {
        if (this.mCurrentTime == null) {
            TimeZone currentTz = TimeZone.getDefault();
            long currentTimeUtc = System.currentTimeMillis();
            this.mCurrentTime = UnsignedInteger.valueOf((((long) currentTz.getOffset(currentTimeUtc)) + currentTimeUtc) / 1000);
        }
        return this.mCurrentTime;
    }

    public synchronized byte[] getBytes() {
        if (!areBytesCached()) {
            addBytes(Byte.valueOf(TimeCommand.SET_TIME_REQ.getCommand()));
            addBytes(ByteUtils.unsignedInt2bytes(getCurrentTime()));
        }
        return super.getBytes();
    }
}
